package com.springcloud.demo.prac_summary;

/**
 * @author zhouyang
 * @Description
 * @date 2020/11/24
 */
public class join {

    private static volatile Integer i = 0;

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            for (int j = 0; j < 100; j++) {
                try {
                    System.out.println(Thread.currentThread().getName());
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (i) {
                    i++;
                }
            }
        });
        thread1.start();
        Thread thread = new Thread(() -> {
            for (int j = 0; j < 100; j++) {
                try {
                    System.out.println(Thread.currentThread().getName());
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (i) {
                    i++;
                }
            }
        });
        thread.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(i);
    }

}
